%%%-------------------------------------------------------------------
%%% File    : stun_packet.erl
%%% Author  : Anil Kumar S. R. <anil.kumar@geodesic.com>
%%% Description : 
%%%
%%% Created : 11 Jan 2010 by Anil Kumar S. R. <anil.kumar@geodesic.com>
%%%-------------------------------------------------------------------
-module(stun_packet).
-export([generate_header/2, add_attribute/3, generate_transaction_id/0]).

-include("stun_header.hrl").

generate_header(Message_Type, Message_Length) ->
    A = <<0:2, Message_Type:14, Message_Length:16, 16#2112A442:32>>,
    B = generate_transaction_id(),
    <<A/binary, B/binary>>.

add_attribute(Attribute_Type, Attribute_Length, Attribute_Value) ->
    A = <<Attribute_Type:16, Attribute_Length:16>>,
    B = to_binary(Attribute_Value),
    <<A/binary, B/binary>>.

generate_transaction_id() ->
    crypto:rand_bytes(12).

to_binary(Value) when is_list(Value) ->
    list_to_binary(Value);
to_binary(Value) when is_bitstring(Value) ->
    Value.
